package com.czh.auth.api.entity;


import com.czh.auth.api.enums.ResponseResultEnum;
import lombok.Data;

import java.io.Serializable;

/**
 * @author czh01
 * @version 1.0
 * @description: 响应结果集 TODO tip
 * @date 2024/7/26 上午 12:19
 */
@Data
//@JsonInclude(JsonInclude.Include.NON_NULL)
public class ResponseResult<T> implements Serializable {

    private int code;

    private String message;

    private Boolean success;

    private T data;

    public ResponseResult(){
        this.code= ResponseResultEnum.SUCCESS.getCode();
        this.message = ResponseResultEnum.SUCCESS.getMsg();
        this.success = true;
    }
    public static ResponseResult<Object> success(){
        return new ResponseResult<>();
    }
    public static<T> ResponseResult<T> success(T data){
        ResponseResult<T> result = new ResponseResult<>();
        result.setData(data);
        return result;
    }
    public static<T> ResponseResult<T> success(String message){
        ResponseResult<T> result = new ResponseResult<>();
        result.setMessage(message);
        return result;
    }
    public static<T> ResponseResult<T> error(ResponseResultEnum responseResultEnum){
        ResponseResult<T> result = new ResponseResult<>();
        result.setError(responseResultEnum.getCode(),responseResultEnum.getMsg(),null);
        return result;
    }
    public static<T> ResponseResult<T> error(String msg){
        ResponseResult<T> result = new ResponseResult<>();
        result.setError(ResponseResultEnum.FAILED.getCode(), msg,null);
        return result;
    }
    public static<T> ResponseResult<T> error(ResponseResultEnum responseResultEnum,T data){
        ResponseResult<T> result = new ResponseResult<>();
        result.setError(responseResultEnum.getCode(),responseResultEnum.getMsg(),data);
        return result;
    }


    private  void setError(int  code,String msg,T data){
        this.code= code;
        this.message = msg;
        this.success = false;
        this.data = data;
    }
    private void setSuccess(ResponseResultEnum responseResultEnum,T data){
        this.code= responseResultEnum.getCode();
        this.message = responseResultEnum.getMsg();
        this.success = true;
        this.data = data;
    }
}
